package com.ys.controller.web.sys;

import com.ys.config.MenuId;
import com.ys.entity.business.TpAmazonShopExchangeRate;
import com.ys.entity.sys.TbCompanyBankInfo;
import com.ys.entity.sys.YsMenuNo;
import com.ys.mapper.sys.YsMenuNoMapper;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/web/sys/tbCompanyBankInfo")
public class WebTbCompanyBankInfoController {
	

	@Autowired
	private CommonApiService<TbCompanyBankInfo> commonApiService;

	@Resource
	private YsMenuNoMapper ysMenuNoMapper;


	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String vid = "";
			YsMenuNo entity = new YsMenuNo().setMenuId(MenuId.TB_CompanyBankInfo).queryFirst();
			if (entity == null) {
				throw new Exception("尚未设置单号格式");
			} else {
				Integer currentNo = entity.getCurrentNo();
				String connectCode = entity.getConnectCode();
				String preCode = entity.getPreCode();
				// 序号+1
				entity.setCurrentNo(currentNo + 1);
				ysMenuNoMapper.update(entity);
				String currentNoNew = String.format("%04d",currentNo);
				vid =  preCode + connectCode + currentNoNew;
			}
			TpAmazonShopExchangeRate tpAmazonShopExchangeRate = new TpAmazonShopExchangeRate().setCode("CNY").queryFirst();
			if (tpAmazonShopExchangeRate == null){
				return W.error(model, "CNY货币未维护，请联系客服");
			}
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("rateCode", tpAmazonShopExchangeRate.getCode());
			return commonApiService.toAdd(model, new TbCompanyBankInfo().setVid(vid).setRateId(tpAmazonShopExchangeRate.getId()).setStatus(1), MenuId.TB_CompanyBankInfo, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}



	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		if (params.get("id") == null){
			return W.error(model, "id不能为空");
		}

		Long id = Long.valueOf((params.get("id").toString()));

		Map<String, Object> extraParam = new TbCompanyBankInfo().setId(id)
				.addLeftJoin("TS_UserInfo B on  B.UserId = A.UserId")
				.addLeftJoin("TP_AmazonShopExchangeRate C on  C.id = A.rateId")
				.addField("B.userName,B.roleName,C.code rateCode")
				.addLeftJoin("TB_CompanyInfo ci ON ci.Vid = A.CompanyInfoVid")//所属公司
				.addField("ci.Vname AS companyInfoName")//
				.queryFirstMap();
		if (extraParam == null){
			return W.error(model, "该公司银行信息不存在!");
		}

		try {
			return commonApiService.toEdit(model, id, new TbCompanyBankInfo().setId(id), MenuId.TB_CompanyBankInfo, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}

}
